<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        list-style: none;
    }

    .box {
        width: 500px;
    }

    .tabList {
        width: 500px;
        text-align: center;
    }

    li {

        width: 100%;
        border: 1px solid skyblue;
        height: 30px;
        margin: 20px auto;
        cursor: pointer;
    }

    .active {
        background-color: red;
    }
</style>

<body>

    <div class="box">
        <ul class="tabList">
            <li>这是第1</li>
            <li>这是第2</li>
            <li>这是第3</li>
            <li>这是第4</li>
            <li>这是第5</li>
            <li>这是第6</li>
        </ul>
    </div>
    <script>

        /*let tabList = document.querySelectorAll('.tabList > li')
            for (let i = 0; i < tabList.length; i++) {
                tabList[i].onclick = function () {
                    for (var j = 0; j < tabList.length; j++) {
                      tabList[j].className = ''
                    }
                 this.className = 'active'
             }
        } */


        let tabList = document.querySelectorAll('.tabList > li');

        for (var i = 0; i < tabList.length; i++) {
            tabList[i].myIndex = i;
            tabList[i].onclick = function () {
                /*        for (var j = 0; j < tabList.length; j++) {
                           tabList[j].className = ''
                       } */
                // this.className = 'active'

                alert(this.myIndex + 1)

            }
        }


    </script>

</body>

</html>